package org.example.rental.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.rental.common.Result;
import org.example.rental.domain.RentalGoods;
import org.example.rental.domain.Vehicle;
import org.example.rental.domain.vo.CarGoodsVo;
import org.example.rental.service.RentalGoodsService;
import org.example.rental.mapper.RentalGoodsMapper;
import org.example.rental.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author xulongxing
* @description 针对表【rental_goods(租赁上架表)】的数据库操作Service实现
* @createDate 2023-12-21 15:45:44
*/
@Service
public class RentalGoodsServiceImpl extends ServiceImpl<RentalGoodsMapper, RentalGoods>
    implements RentalGoodsService{


    @Autowired
    VehicleService vehicleService;


    public RentalGoods toVehicleGoods(Integer vehId) {
        Vehicle vehicle = vehicleService.getById(vehId);
        RentalGoods goods = new RentalGoods();
        //默认主标题
        goods.setVehId(vehicle.getId());
        goods.setVehNum(vehicle.getVehNum());
        goods.setHeadImg(vehicle.getHeadImg());
        goods.setHeading(vehicle.getName()+" "+vehicle.getModelNum());
        goods.setBrandId(vehicle.getBrandId());
        goods.setBrandName(vehicle.getBName());

        StringBuilder sb = new StringBuilder("");
        if (StrUtil.isNotBlank(vehicle.getCompartments())){
            sb.append(vehicle.getCompartments()).append(" | ");
        }
        if (StrUtil.isNotBlank(vehicle.getDisplacement())){
            sb.append(vehicle.getDisplacement()).append(" | ");
        }
        if (StrUtil.isNotBlank(vehicle.getMailbox())){
            sb.append(vehicle.getMailbox()).append(" | ");
        }
        if (StrUtil.isNotBlank(vehicle.getMileage())){
            sb.append(vehicle.getMileage()).append(" | ");
        }
        goods.setSubtitle(sb.toString());
        return goods;
    }


    @Transactional
    public void addGoods(RentalGoods goods) {
        Vehicle vehicle = vehicleService.getById(goods.getVehId());
        goods.setModelYear(vehicle.getModelYear());
        goods.setStatus(1);
        this.save(goods);
        vehicleService.update(new UpdateWrapper<Vehicle>().set("status",1).eq("id",goods.getVehId()));
    }


    @Transactional
    public void deleteByIds(List<Long> list) {

        this.removeByIds(list);
        List<RentalGoods> collect = list.stream().map(id -> {
            RentalGoods goods = this.getById(id);
            return goods;
        }).collect(Collectors.toList());
        collect.forEach(item->{
            Vehicle vehicle = vehicleService.getById(item.getVehId());
            vehicle.setStatus(0);
            vehicleService.updateById(vehicle);
        });

    }


    public CarGoodsVo findCarDetail(Integer id) {
        RentalGoods goods = this.getById(id);
        Vehicle vehicle = vehicleService.getById(goods.getVehId());
        CarGoodsVo carGoodsVo = new CarGoodsVo();
        carGoodsVo.setId(goods.getId());
        carGoodsVo.setVehId(goods.getVehId());
        carGoodsVo.setHeading(goods.getHeading());
        carGoodsVo.setHeadImg(goods.getHeadImg());
        carGoodsVo.setBrandName(goods.getBrandName());
        carGoodsVo.setBlurb(goods.getBlurb());
        carGoodsVo.setSizes(goods.getSizes());
        carGoodsVo.setUseSizes(goods.getUseSizes());

        carGoodsVo.setModelYear(vehicle.getModelYear());
        carGoodsVo.setName(vehicle.getName());
        carGoodsVo.setModelNum(vehicle.getModelNum());
        carGoodsVo.setCategory(vehicle.getCategory());
        carGoodsVo.setDisplacement(vehicle.getDisplacement());
        carGoodsVo.setCompartments(vehicle.getCompartments());
        carGoodsVo.setSeats(vehicle.getSeats());
        carGoodsVo.setDoors(vehicle.getDoors());
        carGoodsVo.setFuel(vehicle.getFuel());
        carGoodsVo.setFuelNum(vehicle.getFuelNum());
        carGoodsVo.setMailbox(vehicle.getMailbox());
        carGoodsVo.setMileage(vehicle.getMileage());

        return carGoodsVo;
    }


    public List<RentalGoods> findHomeSell() {
        //推荐车型
        return this.baseMapper.findHomeSell();
    }


}




